import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter_tool/modle/user.dart';
import 'package:my_flutter_tool/widgets/app_list_tile.dart';
import 'package:my_flutter_tool/widgets/refresh_load_view.dart';

import 'package:my_flutter_tool/util/app_util.dart';

///刷新加载视图
class RefreshLoadTest extends StatelessWidget {
  const RefreshLoadTest({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('刷新或加载'),
      ),
      body: RefreshLoadView<User>(
        pageSize: 20,
        itemBuilder: (i, u) => _buildItem(i, u),
        onRefresh: (size) async {
          await Future.delayed(const Duration(seconds: 1), () {});
          return await Stream.fromIterable(
                  List<int>.generate(size, (i) => i + 1))
              .map((e) => User(id: e))
              .toList();
        },
        onLoad: (page, size) async {
          await Future.delayed(const Duration(seconds: 1), () {});

          if (page >= 5) return [];

          return await Stream.fromIterable(
                  List<int>.generate(size, (i) => i + 1))
              .map((e) => User(id: e))
              .toList();
        },
      ),
    );
  }

  ///构建项目
  _buildItem(int index, User user) => AppListTile(
        padding: EdgeInsets.symmetric(vertical: 60.w, horizontal: 40.w),
        leading: Padding(
          padding: EdgeInsets.only(right: 20.w),
          child: ClipOval(
            child: CachedNetworkImage(
                imageUrl: user.headUrl, width: 100.w, height: 100.w),
          ),
        ),
        title: Text('Item ${user.id}'),
        subTitle: Text('xxxxxxxxxxxxxxxxxxx'),
      );
}
